package com.hlf.modules.merchant.goods.controller;

import com.alibaba.fastjson.JSON;
import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.entity.Print;
import com.hlf.modules.api.service.MachineService;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.entity.GoodsType;
import com.hlf.modules.merchant.goods.service.GoodsService;
import com.hlf.modules.merchant.goods.service.GoodsTypeService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品列表
 */
@RestController
@RequestMapping("goods/")
public class GoodsController extends AbstractController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private MachineService machineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//	@RequiresPermissions("store:view")
    public ReturnResult list(@RequestParam Map<String, Object> params) {
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        String storeId = user.getCurrentStoreId();
        params.put("storeId", storeId);
        params.put("userId", userId);
        PageUtils pageUtil = goodsService.queryListForPage(params);
        return ReturnResult.ok().put("page", pageUtil);
    }

    /**
     * 类型列表
     */
    @RequestMapping("/typeList")
//	@RequiresPermissions("store:view")
    public ReturnResult typeList(@RequestParam Map<String, Object> params) {
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        String storeId = user.getCurrentStoreId();
        params.put("storeId", storeId);
        params.put("userId", userId);
        PageUtils pageUtil = goodsTypeService.queryListForPage(params);
        return ReturnResult.ok().put("page", pageUtil);
    }

    /**
     * 通过id查找商品信息
     */
    @RequestMapping("/info/{goodsId}")
    public ReturnResult config(@PathVariable("goodsId") String goodsId) {
        Goods goods = goodsService.queryObject(Long.parseLong(goodsId));
        return ReturnResult.ok().put("goods", goods);
    }

    /**
     * 通过goodsId查找商品类型
     */
    @RequestMapping("/goodsType/{id}")
    public ReturnResult goodsType(@PathVariable("id") String id) {
        GoodsType goodsType = goodsTypeService.queryObject(Long.parseLong(id));
        return ReturnResult.ok().put("goodsType", goodsType);
    }

    /**
     * 通过goodsId查找商品类型
     */
    @RequestMapping("/goodsTypeList")
    public ReturnResult goodsTypeList() {
        SysUserEntity user = getUser();
        List<GoodsType> goodsTypes = goodsTypeService.queryListForStoreId(user.getCurrentStoreId());
        return ReturnResult.ok().put("goodsTypes", goodsTypes);
    }

    /**
     * 新增商品
     */
    @RequestMapping("/saveGoods")
    public ReturnResult saveGoods(@RequestBody Goods goods) {
        Assert.isNull(goods.getGoodsType(), "商品类型id不能为空");
        Assert.isBlank(goods.getName(), "商品名称不能为空");
        Assert.isNull(goods.getSellPrice(), "商品价格不能为空");
        Assert.isNull(goods.getInitQty(), "初始库存数不能为空");

        SysUserEntity user = getUser();
        goodsService.create(user,goods);
        return ReturnResult.ok();
    }

    /**
     * 删除商品
     */
    @RequestMapping("/delete")
    public ReturnResult saveGoods(@RequestBody Long[] goodsIds) {
        Assert.isNull(goodsIds, "商品id不能为空");

        Long userId = getUserId();
        logger.info("批量删除的id{}"+JSON.toJSONString(goodsIds));
        int i = goodsService.batchUpdateDel(goodsIds, userId);
        if (i == 0) {
            return ReturnResult.error("删除失败!");
        }
        return ReturnResult.ok();
    }

    /**
     * 修改商品
     */
    @RequestMapping("/updateGoods")
    public ReturnResult updateGoods(@RequestBody Goods goods) {
        Assert.isNull(goods.getGoodsType(), "商品类型id不能为空");
        Assert.isNull(goods.getId(), "商品id不能为空");
        Assert.isBlank(goods.getName(), "商品名称不能为空");
        Assert.isNull(goods.getSellPrice(), "商品价格不能为空");
        Assert.isNull(goods.getRemainQty(), "库存数不能为空");
        Assert.isNull(goods.getInitQty(), "初始库存数不能为空");
        logger.warn("修改的商品id{}"+goods.getId());
        SysUserEntity user = getUser();
        goodsService.updateGoods(user,goods);
        return ReturnResult.ok();
    }

    @SysLog("上架")
    @RequestMapping("/up")
    public ReturnResult up(@RequestBody Long[] goodsIds, HttpServletRequest request) {
        Assert.isNull(goodsIds, "商品id不能为空");
        logger.warn("操作的商品id{}"+JSON.toJSONString(goodsIds));
        for (Long goodsId : goodsIds) {
            goodsService.updateGoodsStatus(String.valueOf(goodsId), String.valueOf(Constants.GOODS_STATUS_SHELVE), request, null);
        }
        return ReturnResult.ok();
    }

    @SysLog("下架")
    @RequestMapping("/down")
    public ReturnResult down(@RequestBody Long[] goodsIds, HttpServletRequest request) {
        Assert.isNull(goodsIds, "商品id不能为空");
        logger.warn("操作的商品id{}"+JSON.toJSONString(goodsIds));
        for (Long goodsId : goodsIds) {
            goodsService.updateGoodsStatus(String.valueOf(goodsId), String.valueOf(Constants.GOODS_STATUS_UNSHELVE), request, null);
        }
        return ReturnResult.ok();
    }

    /**
     * 打印机列表
     */
    @RequestMapping("/printList")
    public ReturnResult prinList() {
        SysUserEntity user = getUser();
        Long storeId = Long.parseLong(user.getCurrentStoreId());
        Long userId = user.getUserId();

        List<Print> printList = machineService.queryList(storeId,userId);
        return ReturnResult.ok().put("printList", printList);
    }


    /**
     * 新建商品类型
     */
    @SysLog("新建商品类型")
    @PostMapping("saveGoodsType")
    public ReturnResult saveGoodsType(@RequestBody GoodsType goodsType) {
        Assert.isBlank(goodsType.getName(), "商品类型名称不能为空");
        Assert.isNull(goodsType.getPrinterId(), "打印机id不能为空");
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        goodsType.setId(IdWorkerFactory.nextId());
        goodsType.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        goodsType.setInsertBy(userId);
        goodsTypeService.insertSelective(goodsType);
        return ReturnResult.ok();
    }

    /**
     * 查询单个商品类型
     */
    @RequestMapping("objType/{goodsTypeId}")
    public ReturnResult goodsTypeObj(@PathVariable("goodsTypeId") String goodsTypeId) {
        Assert.isBlank(goodsTypeId, "商品类型id不能为空");
        GoodsType goodsType = goodsTypeService.queryObject(Long.parseLong(goodsTypeId));
        return ReturnResult.ok().put("goodsType",goodsType);
    }


    /**
     * 修改商品类型
     */
    @SysLog("修改商品类型")
    @RequestMapping("updateType")
    public ReturnResult updateGoodsType(@RequestBody GoodsType goodsType) {
        Assert.isBlank(goodsType.getName(), "商品类型名称不能为空");
        Assert.isNull(goodsType.getId(), "商品类型id不能为空");
        goodsTypeService.updateByPrimaryKeySelective(goodsType);
        return ReturnResult.ok();
    }
    /**
     * 批量逻辑删除商品类型
     */
    @SysLog("批量删除商品类型")
    @PostMapping("delType")
    public ReturnResult batchDeleteType(@RequestBody Long[] goodsIds, HttpServletRequest request) {
        Assert.isNull(goodsIds, "商品类型id不能为空");
        logger.info("批量删除的商品类型id{}"+ JSON.toJSONString(goodsIds));
        Long userId = getUserId();
        List<Long> asList = Arrays.asList(goodsIds);
        int i = goodsTypeService.batchUpdateDel(asList, userId);
        if (i == 0) {
            return ReturnResult.error("删除失败!");
        }
        return ReturnResult.ok();
    }



}
